﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BillingSystem.DataSetMainTableAdapters;

namespace BillingSystem
{
    public partial class FormEditDirections : Form
    {
        private BindingSource directionsBindingSource = null;

        /// <summary> Главный DataSet </summary>
        private DataSetMain dataSetMain;

        private DIRECTIONSTableAdapter directionsTableAdapter;

        public FormEditDirections(DataSetMain dataSetMain, DIRECTIONSTableAdapter ad)
        {
            this.directionsTableAdapter = ad;
            this.dataSetMain = dataSetMain;
            InitializeComponent();
        }

        public bool Show(BindingSource dir)
        {
            directionsBindingSource = dir;

            if (directionsBindingSource != null)
            {
                SetGrid();
                this.ShowDialog();
            }
            return true;
        }

        private void SetGrid()
        {
            DataGridViewTextBoxColumn nameDataGridViewTextBoxColumn = new DataGridViewTextBoxColumn();
            DataGridViewTextBoxColumn prefixDataGridViewTextBoxColumn = new DataGridViewTextBoxColumn();
            // 
            // nameDataGridViewTextBoxColumn
            // 
            nameDataGridViewTextBoxColumn.DataPropertyName = "NAME";
            //nameDataGridViewTextBoxColumn.DataSource = this.directionsBindingSource;
            //nameDataGridViewTextBoxColumn.DisplayMember = "NAME";
            //nameDataGridViewTextBoxColumn.DisplayStyle = System.Windows.Forms.DataGridViewComboBoxDisplayStyle.Nothing;
            nameDataGridViewTextBoxColumn.HeaderText = "Название";
            nameDataGridViewTextBoxColumn.Name = "nameDataGridViewTextBoxColumn";
            nameDataGridViewTextBoxColumn.ReadOnly = true;
            nameDataGridViewTextBoxColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
            //nameDataGridViewTextBoxColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
            //nameDataGridViewTextBoxColumn.ValueMember = "DIRECTION_ID";
            nameDataGridViewTextBoxColumn.Width = 100;
            // 
            // prefixDataGridViewTextBoxColumn
            // 
            prefixDataGridViewTextBoxColumn.DataPropertyName = "PREFIX";
            //prefixDataGridViewTextBoxColumn.DataSource = this.directionsBindingSource;
            //prefixDataGridViewTextBoxColumn.DisplayMember = "PREFIX";
            //prefixDataGridViewTextBoxColumn.DisplayStyle = System.Windows.Forms.DataGridViewComboBoxDisplayStyle.Nothing;
            prefixDataGridViewTextBoxColumn.HeaderText = "Код";
            prefixDataGridViewTextBoxColumn.Name = "prefixDataGridViewTextBoxColumn";
            prefixDataGridViewTextBoxColumn.ReadOnly = true;
            prefixDataGridViewTextBoxColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
            //prefixDataGridViewTextBoxColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
            //prefixDataGridViewTextBoxColumn.ValueMember = "DIRECTION_ID";
            prefixDataGridViewTextBoxColumn.Width = 80;
            //
            // dataGridViewDirections
            //
            dataGridViewDirections.AllowUserToAddRows = false;
            dataGridViewDirections.AllowUserToDeleteRows = false;
            dataGridViewDirections.AllowUserToResizeRows = false;
            dataGridViewDirections.AutoGenerateColumns = false;
            dataGridViewDirections.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            dataGridViewDirections.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
            prefixDataGridViewTextBoxColumn,
            nameDataGridViewTextBoxColumn});
            dataGridViewDirections.DataSource = this.directionsBindingSource;
            dataGridViewDirections.MultiSelect = false;
            dataGridViewDirections.ReadOnly = true;
            dataGridViewDirections.RowHeadersVisible = false;
            dataGridViewDirections.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                directionsBindingSource.AddNew();
                FormEditDirection fetn =
                    new FormEditDirection();
                if (fetn.Show(directionsBindingSource))
                {
                    directionsBindingSource.EndEdit();
                    directionsTableAdapter.Update(dataSetMain.DIRECTIONS);
                    directionsTableAdapter.Fill(dataSetMain.DIRECTIONS);
                    directionsBindingSource.MoveLast();
                    MessageBox.Show(((DataRowView)directionsBindingSource.Current)["DIRECTION_ID"].ToString());
                }
            }
            catch (ConstraintException exc)
            {
                MessageBox.Show("Не удалось изменить номер");
                directionsBindingSource.CancelEdit();
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            try
            {
                //directionsBindingSource.AddNew();
                FormEditDirection fetn =
                    new FormEditDirection();
                if (fetn.Show(directionsBindingSource))
                {
                    directionsBindingSource.EndEdit();
                    directionsTableAdapter.Update(dataSetMain.DIRECTIONS);
                    //directionsTableAdapter.Fill(dataSetMain.DIRECTIONS);
                    //directionsBindingSource.MoveLast();
                    ///MessageBox.Show(((DataRowView)directionsBindingSource.Current)["DIRECTION_ID"].ToString());
                }
            }
            catch (ConstraintException exc)
            {
                MessageBox.Show("Не удалось изменить номер");
                directionsBindingSource.CancelEdit();
            }
        }
    }
}